package com.xbts.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.mapper.SeiDataDictMapper;
import com.xbts.system.domain.SeiDataDict;
import com.xbts.system.service.ISeiDataDictService;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;

/**
 * 数据字典对象Service业务层处理
 * 
 * @author xbts
 * @date 2023-09-11
 */
@Service
public class SeiDataDictServiceImpl  extends ServiceImpl<SeiDataDictMapper, SeiDataDict> implements ISeiDataDictService {

    /**
     * 查询数据字典对象列表
     *
     * @param seiDataDict 数据字典对象
     * @return 数据字典对象
     */
    @Override
    public List<SeiDataDict> selectSeiDataDictList(SeiDataDict seiDataDict) {
        QueryWrapper<SeiDataDict> queryWrapper = seiDataDict.queryWrapper();
        return list(queryWrapper);
    }

    /**
     * 查询数据字典对象
     *
     * @param dataDictId 数据字典对象主键
     * @return 数据字典对象
     */
    @Override
    public SeiDataDict selectSeiDataDictByDataDictId(String dataDictId) {
        return getById(dataDictId);
    }

    /**
     * 新增数据字典对象
     *
     * @param seiDataDict 数据字典对象
     * @return 结果
     */
    @Override
    public boolean insertSeiDataDict(SeiDataDict seiDataDict) {
        seiDataDict.setCreateBy(SecurityUtils.getUserId().toString());
        return save(seiDataDict);
    }

    /**
     * 修改数据字典对象
     *
     * @param seiDataDict 数据字典对象
     * @return 结果
     */
    @Override
    public boolean updateSeiDataDict(SeiDataDict seiDataDict) {
        seiDataDict.setUpdateBy(SecurityUtils.getUserId().toString());
        seiDataDict.setUpdateTime(DateUtils.getNowDate());
        return updateById(seiDataDict);
    }

    /**
     * 批量删除数据字典对象
     *
     * @param dataDictIds 需要删除的数据字典对象主键
     * @return 结果
     */
    @Override
    public boolean deleteSeiDataDictByDataDictIds(List<String> dataDictIds) {
        for (String dataDictId : dataDictIds) {
            long count = this.count(new LambdaQueryWrapper<SeiDataDict>()
                    .eq(SeiDataDict::getParentId, dataDictId));
            if(count > 0){
                throw new SecurityException("存在子级，不可删除！");
            }
        }

        return removeBatchByIds(dataDictIds);
    }

    /**
     * 删除数据字典对象信息
     *
     * @param dataDictId 数据字典对象主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteSeiDataDictByDataDictId(String dataDictId) {
      return removeById(dataDictId);
    }

}
